/* 
 *  Copyright (C) 2008 Jens Meiß
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package com.googlecode.hmvc4gwt.example.hmvcblog.frontend.blog.client.blog.blogentry;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.blog.client.blog.blogentry.comment.BlogEntryCommentController;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.blog.client.blog.blogentry.comment.BlogEntryCommentModel;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.blog.client.blog.blogentry.comment.BlogEntryCommentView;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.blog.client.blog.blogentry.comment.events.in.ShowBlogEntryComment;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.blog.client.blog.events.in.AddBlogEntryEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.blog.entry.BlogEntryIdDTO;
import com.googlecode.hmvc4java.component.Component;
import com.googlecode.hmvc4java.event.IEvent;
import com.googlecode.hmvc4java.registry.controller.AControllerModelViewRegistry;

/**
 * The BlogEntryController 
 *
 * @author jens
 */
public class BlogEntryController extends AControllerModelViewRegistry<BlogEntryModel, BlogEntryView> {

	public void handleEvent(IEvent event) {
	}

	public void init() {
		
		BlogEntryIdDTO blogEntryIdDTO = new BlogEntryIdDTO();
		blogEntryIdDTO.setId(getModel().getBlogEntryDTO().getId());
		
		addChildComponent(new Component(new BlogEntryCommentModel(blogEntryIdDTO), new BlogEntryCommentView(), new BlogEntryCommentController()));
		fireEventDown(new ShowBlogEntryComment(getView().getVerticalPanelContainer()));
		
		fireEventUp(new AddBlogEntryEvent(getView()));
		
	}

	public void initModel() {		
	}

	public void initView() {
		
		Label labelDate = new Label(DateTimeFormat.getMediumDateFormat().format(getModel().getBlogEntryDTO().getDate()));
		getView().setLabelDate(labelDate);
				
		Label labelTitle = new Label(getModel().getBlogEntryDTO().getTitle());
		getView().setLabelTitle(labelTitle);
		
		HorizontalPanel horizontalPanelTitle = new HorizontalPanel();
		getView().setHorizontalPanelTitle(horizontalPanelTitle);
		
		Label labelText = new Label(getModel().getBlogEntryDTO().getText());
		getView().setLabelText(labelText);
		
		VerticalPanel verticalPanelContainer = new VerticalPanel();
		verticalPanelContainer.setStylePrimaryName("blogEntry");
		getView().setVerticalPanelContainer(verticalPanelContainer);
		
		/*
		 * build view
		 */
		horizontalPanelTitle.add(labelDate);
		horizontalPanelTitle.add(labelTitle);
		
		verticalPanelContainer.add(horizontalPanelTitle);
		verticalPanelContainer.add(labelText);
		
	}

	public void terminate() {
	}

}
